package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.param.IdParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.vxaddressvo.VXAddressListVO;
import com.cskaoyan.exception.ValidationException;
import com.cskaoyan.service.vxaddress.VXAddressService;
import com.cskaoyan.util.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 创建日期: 2022/05/07 15:46
 *
 * @author shuyang
 */
@RestController
@RequestMapping("wx/address")
public class AddressWXController {
    @Autowired
    VXAddressService vxAddressService;

    @GetMapping("list")
    public BaseRespVo<VXAddressListVO> queryAddressListInVX() {
        VXAddressListVO vxAddressListVO = vxAddressService.queryAddressListInVX();
        return BaseRespVo.ok(vxAddressListVO);
    }

    @GetMapping("detail")
    public BaseRespVo<MarketAddress> queryAddressByIdInVX(Integer id) {
        MarketAddress marketAddress = vxAddressService.queryAddressByIdInVX(id);
        return BaseRespVo.ok(marketAddress);
    }

    @PostMapping("save")
    //如果运行不对，手动设置id为null
    public BaseRespVo<Integer> saveAddressInVX(@RequestBody @Validated MarketAddress marketAddress, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        vxAddressService.saveAddressInVX(marketAddress);
        return BaseRespVo.ok(marketAddress.getId());
    }

    @PostMapping("delete")
    public BaseRespVo deleteAddressInVX(@RequestBody IdParam param) {
        Integer id = param.getId();
        vxAddressService.deleteAddressInVX(id);
        return BaseRespVo.ok(null);
    }

}
